package com.example.avmanagerweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.avmanagerweb.Utils.List.ListSortUtil;
import com.example.avmanagerweb.entity.Fence;
import com.example.avmanagerweb.entity.Fenceshape;
import com.example.avmanagerweb.entity.Fenceshapepos;
import com.example.avmanagerweb.entity.vo.FenceGraphVo;
import com.example.avmanagerweb.entity.vo.FenceListVo;
import com.example.avmanagerweb.entity.vo.FencePTVo;
import com.example.avmanagerweb.entity.vo.FenceVo;
import com.example.avmanagerweb.mapper.FenceMapper;
import com.example.avmanagerweb.result.exceptionhandler.JWException;
import com.example.avmanagerweb.service.FenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.avmanagerweb.service.FenceshapeService;
import com.example.avmanagerweb.service.FenceshapeposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2023-06-27
 */
@Service
public class FenceServiceImpl extends ServiceImpl<FenceMapper, Fence> implements FenceService {

    @Resource
    private FenceMapper fenceMapper;

    @Autowired
    @Lazy
    private FenceService fenceService;
    @Autowired
    @Lazy
    private FenceshapeService fenceshapeService;
    @Autowired
    @Lazy
    private FenceshapeposService fenceshapeposService;


    @Override
    public List<FenceListVo> getFenceList(int depid) {
        return fenceMapper.getFenceList(depid);
    }

    @Override
    @Transactional  //设置事件
    public boolean addFence(FenceVo fenceVo) {
        System.out.println(fenceVo.toString());
        Fence fence = new Fence();
        fence.setDepid(fenceVo.getDepId());
        fence.setFencename(fenceVo.getFenceName());
        boolean save = fenceService.save(fence);
        System.out.println(fenceVo.toString());

        if(save){
            if(fenceVo.getShapetype() == null || fenceVo.getRadius() == null){
                return true;
            }
            Fenceshape fenceshape = new Fenceshape();
            fenceshape.setFenceid(fence.getFenceid());
            fenceshape.setShapetype(fenceVo.getShapetype());
            fenceshape.setRadius(fenceVo.getRadius());
            boolean save1 = fenceshapeService.save(fenceshape);
            if(save1){
                List<FencePTVo> fencePTVos = fenceVo.getFencePTVos();
                if(fencePTVos == null || fencePTVos.size() ==0){
                    throw new JWException(500,"点位异常");
                }
                System.out.println(fencePTVos.get(0));
                ListSortUtil.sort(fencePTVos,"sn","ASC");

                Fenceshapepos fenceshapepos = new Fenceshapepos();
                fenceshapepos.setFenceid(fence.getFenceid());
                fenceshapepos.setFenceshapeid(fenceshape.getFenceshapeid());
                for (FencePTVo fencePTVo : fencePTVos) {
                    fenceshapepos.setLat84(fencePTVo.getLat84());
                    fenceshapepos.setLon84(fencePTVo.getLon84());
                    fenceshapepos.setLatbd(fencePTVo.getLatbd());
                    fenceshapepos.setLonbd(fencePTVo.getLonbd());
                    fenceshapepos.setLatgcj(fencePTVo.getLatgcj());
                    fenceshapepos.setLongcj(fencePTVo.getLongcj());
                    boolean save2 = fenceshapeposService.save(fenceshapepos);
                    if (!save2){
                        throw new JWException(500,"点位添加异常");
                    }
                }
            }
        }
        return true;
    }

    @Override
    @Transactional  //设置事件
    public boolean addFenceGraph(FenceGraphVo fenceGraphVo) {
        System.out.println(fenceGraphVo.toString());
        Fenceshape fenceshape = new Fenceshape();
        fenceshape.setFenceid(fenceGraphVo.getFenceid());
        fenceshape.setShapetype(fenceGraphVo.getShapetype());
        fenceshape.setRadius(fenceGraphVo.getRadius());
        boolean save1 = fenceshapeService.save(fenceshape);
        if(save1){
            List<FencePTVo> fencePTVos = fenceGraphVo.getFencePTVos();
            if(fencePTVos == null || fencePTVos.size() ==0){

                throw new JWException(500,"点位异常");
            }
            System.out.println(fencePTVos.get(0));
            ListSortUtil.sort(fencePTVos,"sn","ASC");

            Fenceshapepos fenceshapepos = new Fenceshapepos();
            fenceshapepos.setFenceid(fenceGraphVo.getFenceid());
            fenceshapepos.setFenceshapeid(fenceshape.getFenceshapeid());
            for (FencePTVo fencePTVo : fencePTVos) {
                fenceshapepos.setLat84(fencePTVo.getLat84());
                fenceshapepos.setLon84(fencePTVo.getLon84());
                fenceshapepos.setLatbd(fencePTVo.getLatbd());
                fenceshapepos.setLonbd(fencePTVo.getLonbd());
                fenceshapepos.setLatgcj(fencePTVo.getLatgcj());
                fenceshapepos.setLongcj(fencePTVo.getLongcj());
                boolean save2 = fenceshapeposService.save(fenceshapepos);
                if (!save2){
                    throw new JWException(500,"点位添加异常");
                }
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean deleteFence(int fenceId) {
        boolean b = fenceService.removeById(fenceId);
        if (b){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("FenceID", fenceId);
            boolean remove = fenceshapeService.remove(wrapper);
            if(remove){
                boolean remove1 = fenceshapeposService.remove(wrapper);
                return remove1;
            }
        }
        return true;
    }

    @Override
    public boolean deleteFenceGraph(int fenceId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("FenceID", fenceId);
        boolean remove = fenceshapeService.remove(wrapper);
        if(remove){
            boolean remove1 = fenceshapeposService.remove(wrapper);
            return remove1;
        }
        return true;
    }
}
